//
//  IntroductionViewController.m
//  suishouxiu
//
//  Created by parkin on 15/7/6.
//  Copyright (c) 2015年 zhongyan. All rights reserved.
//

#import "IntroductionViewController.h"
//tools


static NSInteger totalPages = 4;
@interface IntroductionViewController ()
@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) UIPageControl * pageControl;
@property (nonatomic, strong) UIButton * startButton;
@end
@implementation IntroductionViewController

#pragma mark - life circle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    [self.view addSubview:self.startButton];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.scrollView.frame = CGRectMake(0, 0, K_ScreenWidth, K_ScreenHeight);
    self.pageControl.frame = CGRectMake(0, K_ScreenHeight - 40, 100, 30);
    self.pageControl.center = CGPointMake(self.view.centerX, self.pageControl.centerY);
    self.startButton.frame = CGRectMake(0, 0, 150, 40);
    self.startButton.center = CGPointMake(self.view.centerX, self.pageControl.centerY - 30);
}
#pragma mark - private method
- (NSInteger)getCurrentPageIndex {
    return (NSInteger)(self.scrollView.contentOffset.x / K_ScreenWidth);
}
- (void)scrollToPage:(NSInteger)pageIndex {
    [UIView animateWithDuration:0.25 animations:^{
        _scrollView.contentOffset = CGPointMake(pageIndex * K_ScreenWidth, 0);
    }];
    
}
- (BOOL)isLastPage:(NSInteger)index {
    return index == totalPages - 1;
}
- (void)pageControlToPage:(NSInteger)pageIndex {
    self.pageControl.currentPage = pageIndex;
}
#pragma mark - custom events
- (void)startAction {
    self.view.window.hidden = YES;
    [[NSUserDefaults standardUserDefaults] setObject:@1 forKey:K_FirstStartKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)currentPageChanged:(UIPageControl *)pageControl {
    [self scrollToPage:pageControl.currentPage];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = [self getCurrentPageIndex];
    [self pageControlToPage:index];
    if ([self isLastPage:index] == YES) {
        [UIView animateWithDuration:0.25 animations:^{
            self.startButton.hidden = NO;
            self.pageControl.hidden = YES;
        }];
        
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            self.startButton.hidden = YES;
            self.pageControl.hidden = NO;
        }];
        
    }
}
#pragma mark - setter, getter
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(K_ScreenWidth * totalPages, K_ScreenHeight);
        for (int i = 0; i < totalPages; i++) {
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * K_ScreenWidth, 0, K_ScreenWidth, K_ScreenHeight)];
            imageView.image= [UIImage imageNamed:[NSString stringWithFormat:@"page%d", i + 1]];
            [_scrollView addSubview:imageView];
        }
    }
    return _scrollView;
}

- (UIPageControl *)pageControl {
    if (_pageControl == nil) {
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
        _pageControl.numberOfPages = totalPages;
        
      
        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithHexString:K_Global_ColorHexString_Normal];
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        [_pageControl addTarget:self action:@selector(currentPageChanged:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}

- (UIButton *)startButton {
    
    if (_startButton == nil) {
        self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startButton setTitle:@"立即体验" forState:UIControlStateNormal];
        [_startButton setBackgroundColor:[UIColor colorWithHexString:K_Global_ColorHexString_Normal]];
        [_startButton addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
        _startButton.hidden = YES;
        _startButton.layer.cornerRadius = 4;
        _startButton.layer.masksToBounds = YES;
        
    }
    return _startButton;
}
@end
